import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    port: 3000,
    host: true, // 使其他设备可以访问
    open: true, // 自动打开浏览器
    proxy: {
      '/api/openai-tts': {
        target: 'http://localhost:5001',
        changeOrigin: true,
        secure: false,
        // 增加详细日志
        configure: (proxy, _options) => {
          proxy.on('error', (err, _req, _res) => {
            console.log('代理错误', err);
          });
          proxy.on('proxyReq', (proxyReq, req, _res) => {
            console.log('发送请求到:', req.url);
          });
          proxy.on('proxyRes', (proxyRes, req, _res) => {
            console.log('收到响应:', proxyRes.statusCode, req.url);
          });
        }
      },
      '/api/zhipu-tts': {
        target: 'http://localhost:5002',
        changeOrigin: true,
        secure: false
      },
      '/api/volcano-tts': {
        target: 'http://localhost:5003',
        changeOrigin: true,
        secure: false,
        configure: (proxy, _options) => {
          proxy.on('error', (err, _req, _res) => {
            console.log('火山引擎代理错误', err);
          });
        }
      }
    }
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },
  // 确保构建输出到正确的目录
  build: {
    outDir: 'dist'
  }
})
